﻿Shader "Hidden/Universal Render Pipeline/GlitchRGBSplit"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    
    HLSLINCLUDE
	
	#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl"

	TEXTURE2D(_MainTex);		SAMPLER(sampler_MainTex);

	uniform half4 _Params;
	uniform half3 _Params2;

	#define _Fading _Params.x
	#define _Amount _Params.y
	#define _Speed _Params.z
	#define _CenterFading _Params.w
	#define _TimeX _Params2.x
	#define _AmountR _Params2.y
	#define _AmountB _Params2.z

	half4 FragHorizontal(Varyings i) : SV_Target
	{
		//float2 uv = i.texcoord.xy;
		// TODO 改为使用loadTex 方法进行读取，性能更好 
		float2 uv = i.positionCS.xy;
		half time = _TimeX * 6 * _Speed;
		half splitAmount = (1.0 + sin(time)) * 0.5;
		splitAmount *= 1.0 + sin(time * 2) * 0.5;
		splitAmount = pow(splitAmount, 3.0) * 0.05;
		float distance = length(uv - float2(0.5, 0.5));
		splitAmount *= _Fading * _Amount;
		splitAmount *= lerp(1, distance, _CenterFading);

		half3 colorR = LOAD_TEXTURE2D(_MainTex,  float2(uv.x + splitAmount * _AmountR, uv.y)).rbg;
		half4 sceneColor = LOAD_TEXTURE2D(_MainTex,  uv);
		half3 colorB = LOAD_TEXTURE2D(_MainTex,  float2(uv.x - splitAmount * _AmountB, uv.y)).rbg;

		half3 splitColor = half3(colorR.r, sceneColor.g, colorB.b);
		half3 finalColor = lerp(sceneColor.rgb, splitColor, _Fading);

		return half4(finalColor, 1.0);
	}

	half4 FragVertical(Varyings i) : SV_Target
	{
		float2 uv = i.positionCS.xy;
		half time = _TimeX * 6 * _Speed;
		half splitAmount = (1.0 + sin(time)) * 0.5;
		splitAmount *= 1.0 + sin(time * 2) * 0.5;
		splitAmount = pow(splitAmount, 3.0) * 0.05;
		float distance = length(uv - float2(0.5, 0.5));
		splitAmount *= _Fading * _Amount;
		splitAmount *= lerp(1, distance, _CenterFading);

		half3 colorR = LOAD_TEXTURE2D(_MainTex, float2(uv.x, uv.y + splitAmount * _AmountR)).rgb;
		half4 sceneColor = LOAD_TEXTURE2D(_MainTex, uv);
		half3 colorB = LOAD_TEXTURE2D(_MainTex, float2(uv.x, uv.y - splitAmount * _AmountB)).rbg;

		half3 splitColor = half3(colorR.r, sceneColor.g, colorB.b);
		half3 finalColor = lerp(sceneColor.rgb, splitColor, _Fading);

		return half4(finalColor, 1.0);
	}

	half4 FragHorizontalVertical(Varyings i) : SV_Target
	{
		float2 uv = i.positionCS.xy;
		half time = _TimeX * 6 * _Speed;
		half splitAmount = (1.0 + sin(time)) * 0.5;
		splitAmount *= 1.0 + sin(time * 2) * 0.5;
		splitAmount = pow(splitAmount, 3.0) * 0.05;
		float distance = length(uv - float2(0.5, 0.5));
		splitAmount *= _Fading * _Amount;
		splitAmount *= lerp(1, distance, _CenterFading);

		float splitAmountR = splitAmount * _AmountR;
		float splitAmountB = splitAmount * _AmountB;

		half3 colorR = LOAD_TEXTURE2D(_MainTex, float2(uv.x + splitAmountR, uv.y + splitAmountR)).rbg;
		half4 sceneColor = LOAD_TEXTURE2D(_MainTex, uv);
		half3 colorB = LOAD_TEXTURE2D(_MainTex, float2(uv.x - splitAmountB, uv.y - splitAmountB)).rgb;

		
		half3 splitColor = half3(colorR.r, sceneColor.g, colorB.b);
		half3 finalColor = lerp(sceneColor.rgb, splitColor, _Fading);

		return half4(finalColor, 1.0);
	}

	struct appdata
    {
        float4 vertex : POSITION;
        float2 uv : TEXCOORD0;
    };

    struct v2f
    {
        float2 uv : TEXCOORD0;
        float4 vertex : SV_POSITION;
    };

	 v2f vert(appdata v)
    {
        v2f o;
        o.vertex = TransformObjectToHClip(v.vertex);
        o.uv = v.uv;
        return o;
    }

	ENDHLSL
	
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment FragHorizontal
            ENDHLSL
        }
        Pass
        {
        	HLSLPROGRAM
        	#pragma vertex vert
            #pragma fragment FragVertical
        	ENDHLSL
        }
		Pass
        {
        	HLSLPROGRAM
        	#pragma vertex vert
            #pragma fragment FragHorizontalVertical
        	ENDHLSL
        }
    }
}